using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;
using IGlobalist.Cms.Common;


namespace IGlobalist.Cms.Admin.Control
{

    public partial class ActionBar : AdminBaseUserControl
    {
        public event EventHandler NewButtonClick;
        public event EventHandler SaveButtonClick;
        public event EventHandler CopyButtonClick;
        public event EventHandler DeleteButtonClick;
        public event EventHandler SendEditContentButtonClick;
        public event EventHandler SendAuthorContentButtonClick;
        public event EventHandler SendApproverContentButtonClick;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ibSendApprover.Visible = false;
                ibSendAuthor.Visible=false;
                ibSendEditor.Visible=false;

                if (ObjectTypeCurrent != 0)
                {
                    ibCopy.Visible = false;
                    SetupButton(ActionType.None);
                }
                else
                {
                    SetupButton(ActionType.Add);
                }
                                
                ibDelete.Attributes.Add("onclick","return confirm('Are you sure you want to delete?')");
                
            }
        }



        public bool Register()
        {
            return true;
        }

        public CmsObjectType ObjectTypeCurrent
        {
            get
            {
                if (ViewState["_objectTypeCurrent"] == null)
                    return (CmsObjectType) 0;
                return (CmsObjectType)Enum.Parse(typeof(CmsObjectType), ViewState["_objectTypeCurrent"].ToString());
            }
            set
            {
                ViewState["_objectTypeCurrent"] = value;
            }
        }

        public void SetupButtonApprover(object sender, EventArgs e)
        {
            if (IGlobalistMembership.IsAuthenticate)
            {
                if (IGlobalistMembership.IsInRole(BuiltinRole.ContentAdmin) || IGlobalistMembership.IsInRole(BuiltinRole.SysAdmin))
                {
                    ibSendApprover.Visible = true;
                    ibSendAuthor.Visible =false;
                    ibSendEditor.Visible = false;
                }
                else
                    if (IGlobalistMembership.IsInRole(BuiltinRole.Editor))
                    {
                        ibSendAuthor.Visible = false;
                        ibSendEditor.Visible = false;
                    }
                    else
                        if (IGlobalistMembership.IsInRole(BuiltinRole.Author))
                        {
                            ibSendEditor.Visible = false ;
                        }
            }      
        }

        public void SetupButtonAuthor(object sender, EventArgs e)
        {
            if (IGlobalistMembership.IsAuthenticate)
            {
                if (IGlobalistMembership.IsInRole(BuiltinRole.ContentAdmin) || IGlobalistMembership.IsInRole(BuiltinRole.SysAdmin))
                {
                    ibSendAuthor.Visible = true;
                    ibSendApprover.Visible = false;
                }               
            }
        }

        public void SetupButtonHidden(object sender, EventArgs e)
        {
            ibSendApprover.Visible = false;
            ibSendAuthor.Visible = false;
            ibSendEditor.Visible = false;
        }

        public void SetupButton(ActionType actionType)
        {
            ibNew.Enabled = true;
            ibDelete.Enabled = false;
            //ibSave.Enabled = false;
            ibCopy.Enabled = false;
            ibBack.Visible = false;

            switch (actionType)
            {
                case ActionType.Add:
                    ibSave.Enabled = true;
                    ibDelete.Enabled = false;
                    break;
                case ActionType.Delete:
                    ibSave.Enabled = true;
                    break;
                case ActionType.Edit:
                    ibNew.Enabled = true;
                    ibDelete.Enabled = true;
                    ibSave.Enabled = true;
                    if (ObjectTypeCurrent == 0)
                    {
                        ibCopy.Enabled = true;
                    }
                    break;
                case ActionType.List:
                    ibNew.Enabled = true;
                    ibSave.Enabled = false;
                    ibDelete.Enabled = false;
                    break;
                case ActionType.View:
                    ibNew.Enabled = true;
                    ibSave.Enabled = false;
                    ibDelete.Enabled = false;
                    break;
                case ActionType.EditClass:
                    ibNew.Enabled = true;
                    ibSave.Enabled = true;
                    ibDelete.Enabled = false;
                    break;
                case ActionType.None:
                    ibSave.Enabled = false;
                    ibDelete.Enabled = false;
                    break;
            }

        }

        protected void ibNew_Click(object sender, EventArgs e)
        {
            if (NewButtonClick != null)
            {
                NewButtonClick(this, new EventArgs());
                SetupButton(ActionType.Add);
            }
        }

        protected void ibSave_Click(object sender, EventArgs e)
        {
            if (SaveButtonClick != null)
            {
                SaveButtonClick(this, new EventArgs());
            }
        }

        protected void ibCopy_Click(object sender, EventArgs e)
        {
            if (CopyButtonClick != null)
            {
                CopyButtonClick(this, new EventArgs());
                SetupButton(ActionType.Add);
            }
        }

        protected void ibDelete_Click(object sender, EventArgs e)
        {
            if (DeleteButtonClick != null)
            {
                DeleteButtonClick(this, new EventArgs());

            }
        }

        public void DoClassClick(object sender, EventArgs e)
        {
            
                if (ObjectTypeCurrent == 0)
                {
                    SetupButton(ActionType.Edit);
                }
                else
                {
                    SetupButton(ActionType.List);
                }
                
        }

        public void DoListEditClick(object sender, EventArgs e)
        {
            SetupButton(ActionType.Edit);
        }

        public void DoViewClick(object sender, EventArgs e)
        {
            SetupButton(ActionType.View);
        }

        public void DoEditClick(object sender, EventArgs e)
        {

            SetupButton(ActionType.Add);
        }

        public void DoSaveErrorClick(object sender, EventArgs e)
        {
            IsSaveError = true;
        }

        public void DoEditClassClick(object sender, EventArgs e)
        {

            SetupButton(ActionType.EditClass);
        }

        protected void ibSendEditor_Click(object sender, EventArgs e)
        {
            if (SendEditContentButtonClick != null)
            {
                SendEditContentButtonClick(this, new EventArgs());
            }
        }

        protected void ibSendApprover_Click(object sender, EventArgs e)
        {
            if (SendApproverContentButtonClick != null)
            {
                SendApproverContentButtonClick(this, new EventArgs());
            }
        }

        protected void ibSendAuthor_Click(object sender, EventArgs e)
        {
            if (SendAuthorContentButtonClick != null)
            {
                SendAuthorContentButtonClick(this, new EventArgs());
            }
        }

        #region Property
        private bool _isSaveError = false;
        public bool IsSaveError
        {
            get { return _isSaveError; }
            set { _isSaveError = value; }
        }
        #endregion
    }

}
